package com.lkz.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lkz.entity.EmpRole;
import com.lkz.entity.Employee;
import com.lkz.mapper.EmpRoleMapper;
import com.lkz.mapper.EmployeeMapper;
import com.lkz.service.IEmployeeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lkz.utils.ShiroUtils;
import com.lkz.utils.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;


@Service
@Transactional
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements IEmployeeService {
    @Resource
    private EmployeeMapper employeeMapper;
    @Resource
    private EmpRoleMapper empRoleMapper;

    @Override
    public IPage<Employee> selectList(Page<Employee> page){
        return employeeMapper.selectList(page);
    }

    @Override
    public IPage<Employee> selectByDel(Page<Employee> page) {
        return employeeMapper.selectByDel(page);
    }

    @Override
    public Employee selectByName(String empName) {
        return employeeMapper.selectByName(empName);
    }

    @Override
    public Integer add(Employee employee, Integer roleId) {
        employee.setSalt(ShiroUtils.randomSalt());
        employee.setPwd(ShiroUtils.encryptPassword(employee.getPwd(),employee.getCredentialsSalt()));
        employee.setCreateTime(StringUtils.getNowTime());
        employee.setUpdateTime(employee.getCreateTime());
        employee.setIsDel(0);

        int result = employeeMapper.insert(employee);
        int empId = employee.getEmpId();
        EmpRole empRole = new EmpRole(empId,roleId);
        empRole.setCreateTime(StringUtils.getNowTime());
        empRole.setUpdateTime(empRole.getCreateTime());
        empRole.setIsDel(0);
        empRoleMapper.insert(empRole);

        /*DeptRole deptRole =new DeptRole(deptId,roleId);
        deptRole.setCreateTime(StringUtils.getNowTime());
        deptRole.setUpdateTime(empRole.getCreateTime());
        deptRole.setIsDel(0);
        deptRoleMapper.insert(deptRole);*/
        return result;
    }

    @Override
    public Integer update(Employee employee, Integer roleId) {
        if (!"".equals(employee.getPwd()) ){
            employee.setSalt(ShiroUtils.randomSalt());
            employee.setPwd(ShiroUtils.encryptPassword(employee.getPwd(),employee.getCredentialsSalt()));
        }else {
            employee.setPwd(null);
        }
        employee.setUpdateTime(StringUtils.getNowTime());
        int result =  employeeMapper.updateById(employee);
        EmpRole empRole = new EmpRole(employee.getEmpId(),roleId);
        empRole.setUpdateTime(employee.getUpdateTime());
        System.out.println("----------->"+empRole);
        Integer update = empRoleMapper.update(empRole);
        if(update!=1){
            empRole.setCreateTime(StringUtils.getNowTime());
            empRole.setUpdateTime(empRole.getCreateTime());
            empRole.setIsDel(0);
            empRoleMapper.insert(empRole);
        }
        return result;
    }

    @Override
    public Integer del(Integer empId) {
        Employee employee = new Employee();
        employee.setEmpId(empId);
        employee.setIsDel(1);
        empRoleMapper.del(empId);
        int result = employeeMapper.updateById(employee);

        return result;
    }
    @Override
    public Integer recover(Integer empId) {
        Employee employee = new Employee();
        employee.setEmpId(empId);
        employee.setIsDel(0);
        empRoleMapper.del(empId);
        int result = employeeMapper.updateById(employee);
        return result;
    }

    @Override
    public Integer selectByRoleName(String roleName) {
        return employeeMapper.selectByRoleName(roleName);
    }
}
